/**
 * @file   EventConfirmationPacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-26
 * @brief  DON packet object: Event Confirmation
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/Packet/EventConfirmationPacket.h>
#include <DON/Core/DataType/UInt16.h>
#include <QtCore/QByteArray>

using namespace DON::Core::Packet;
using namespace DON::Core::DataType;

class EventConfirmationPacket::Private
{
public:
    Private()
        : referenceSequence(0u),
          responseStatus(ResponseStatus_Invalid)
    { }

    quint16 referenceSequence;
    ResponseStatus responseStatus;
};

EventConfirmationPacket::EventConfirmationPacket()
    : AbstractPacket(),
      d(new Private())
{
    setPacketType(PacketType_EventConfirmation);
}

EventConfirmationPacket::EventConfirmationPacket(const quint16 source,
                                                 const quint16 destination,
                                                 const quint16 sequence,
                                                 const quint16 referenceSequence,
                                                 const ResponseStatus responseStatus)
    : AbstractPacket(source,
                     destination,
                     PacketType_EventConfirmation,
                     sequence),
      d(new Private())
{
    d->referenceSequence = referenceSequence;
    d->responseStatus = responseStatus;
}

EventConfirmationPacket::EventConfirmationPacket(const EventConfirmationPacket &other)
    : AbstractPacket(other),
      d(new Private())
{
    d->referenceSequence = other.d->referenceSequence;
    d->responseStatus = other.d->responseStatus;
}

EventConfirmationPacket::~EventConfirmationPacket()
{
    delete d;
}

EventConfirmationPacket & EventConfirmationPacket::operator=(const EventConfirmationPacket &other)
{
    AbstractPacket::operator=(other);

    d->referenceSequence = other.d->referenceSequence;
    d->responseStatus = other.d->responseStatus;

    return *this;
}

bool EventConfirmationPacket::isValid() const
{
    return isResponseStatusValid(d->responseStatus);
}

quint16 EventConfirmationPacket::getReferenceSequence() const
{
    return d->referenceSequence;
}

void EventConfirmationPacket::setReferenceSequence(const quint16 referenceSequence)
{
    d->referenceSequence = referenceSequence;
}

ResponseStatus EventConfirmationPacket::getResponseStatus() const
{
    return d->responseStatus;
}

void EventConfirmationPacket::setResponseStatus(const ResponseStatus responseStatus)
{
    d->responseStatus = responseStatus;
}

QList<QByteArray> EventConfirmationPacket::getPayloadDataItems(bool *ok) const
{
    QList<QByteArray> payloadDataItems;
    bool success = false;

    if (isValid())
    {
        success = true;

        // Reference Sequence
        payloadDataItems.append(UInt16::toBinary(d->referenceSequence));

        // Response Status
        const char responseStatus = fromResponseStatusToBinary(d->responseStatus);
        payloadDataItems.append(QByteArray(1, responseStatus));
    }

    if (ok != NULL)
    {
        *ok = success;
    }

    return payloadDataItems;
}
